<?php
/**
 * Controller
 * 
 * Class which links Model classes and the Dispatcher.
 * They filter key/value and returns to the good methods in 
 * the model class
 *
 * @copyright inovia
 * @package oolime_core   
 * 
 */
abstract class Controller {

  protected $debugController;
  public $response;
  public $request;
  public $database;
  public $user;
  public $usergroup;
  public $session;
  public $state;
  public $logs;
   
  /**
  * Constructor
  * @final 
  */
  final function __construct(){
    global $logs;
    global $database;
    global $response;
    global $request;
    global $user;
    global $usergroup;
    global $session;
    global $configDebugController;
    $this->logs           = &$logs;
    $this->response       = &$response;
    $this->request        = &$request;
    $this->database       = &$database;
    $this->user           = &$user;
    $this->usergroup      = &$usergroup;
    $this->session        = &$session;
    $this->debugController  = &$configDebugController;
  
  
    if($this->isLegitimate()){
      $this->state="success";
      $this->actions();
       
    }else{
      $this->state="error";
      $this->defaultActions();
    }
  }


  /**
   * Function returning a boolean specifying if the actions can be done
   *
   * @return bool indicates if the actions can be done
   */
  protected abstract function isLegitimate();
  
  /**
   * Function doing the actions of the Controller
   * 
   */
  protected abstract function actions();
  
  
  /**
   * Function doing default actions of the Controller 
   *
   */
  protected abstract function defaultActions();

  /**
   * @todo to implement
   */     
  final function setLogDetails($details){
      
  }
  
  /**
   * Destructor providing the state of the execution in the response
   */     
  final function __destruct(){
    $node=$this->response->doc->createElement("state");
    $node2=$this->response->doc->createTextNode($this->state);
    $node->appendChild($node2);
    $this->response->add($node);
  }
}
?>
